package com.ruoyi.order.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.match.domain.Match;
import com.ruoyi.match.mapper.MatchMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.service.IOrderService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 订单管理Service业务层处理
 * 
 * @author HuLongHui
 * @date 2023-02-11
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MatchMapper matchMapper;
    /**
     * 查询订单管理
     * 
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public Order selectOrderByOrderId(Long orderId)
    {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     * 
     * @param order 订单管理
     * @return 订单管理
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        order.setCreateBy(SecurityUtils.getUsername());
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        order.setCreateBy(SecurityUtils.getUsername());

        Order o=new Order();
        o.setMatchId(order.getMatchId());
        o.setCreateBy(SecurityUtils.getUsername());
        List<Order> orders = orderMapper.selectOrderList(o);
        if(!CollectionUtils.isEmpty(orders)){
            throw new ServiceException("您已预约该比赛,请勿重复预约!");
        }
        Match match = matchMapper.selectMatchByMatchId(order.getMatchId());
        if (match==null){
            throw new ServiceException("您预约的比赛不存在,请联系管理员!");
        }
        if (match.getMatchMaxPeople()<= match.getMatchAppointmentsPeople()){
            throw new ServiceException("预约人数已满,无法预约!");
        }
        Match m=new Match();
        m.setMatchId(o.getMatchId());
        m.setMatchAppointmentsPeople(match.getMatchAppointmentsPeople()+1);
        matchMapper.updateMatch(m);
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单管理
     * 
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderIds(Long[] orderIds)
    {
        return orderMapper.deleteOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     * 
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderId(Long orderId)
    {
        return orderMapper.deleteOrderByOrderId(orderId);
    }
}
